﻿using System.Collections.Generic;
using System.Diagnostics;
using Newtonsoft.Json;
using System.Web.Configuration;
using JetBrains.Annotations;
using SingleUserMall.Authorization.Users;
using SingleUserMall.Orders;

namespace SingleUserMall.ModelMessage
{
    public class TemplateMessage
    {
        private static TemplateMessage instance;

        private Dictionary<string, TemplateData> datas;
        public static TemplateMessage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new TemplateMessage();
                }
                return instance;
            }
        }
        public string SendTemplateMessage( User user,Order order)
        {
            var postUrl = string.Format(WeiXinHelper.TemplateMessage, AccessToken.Instance);
            var data = new
            {
                first = new TemplateData("您好，欢迎光临五行商城"),
                OrderNo= new  TemplateData(order.OrderNo),
                Freight =new  TemplateData(order.Freight.ToString()),
                Amount = new TemplateData(order.Amount.ToString()),
                PayTime = new TemplateData(order.PayTime.ToString())
            };
            var msgData = new
            {
                touser = user.OpenId,
                template_id = "1",
                data=datas
            };
            string postData = JsonConvert.SerializeObject(msgData);
            var result = HttpClientHelper.PostResponse(postUrl, postData);
            return result;
        }
    }
}
